<?php

namespace addons\shopro\model\commission;

use think\Db;
use think\Model;

class DivisionLog extends Model
{
    // 表名
    protected $name = 'shopro_commission_division_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 追加属性
    protected $append = [
        'createtime_text',
        'username',
        'goods_name',
        'image'
    ];

    public function getCreatetimeTextAttr($val,$data)
    {
        $val = !empty($data['createtime']) ? date('Y-m-d H:i:s',$data['createtime']) : '';
        return $val;
    }

    public function getUsernameAttr($val,$data)
    {
        $user_id = Db::name('shopro_order')->where(['id'=>$data['order_id']])->value('user_id');
        if(!empty($user_id)){
            $user = Db::name('user')->where('id',$user_id)->value('nickname');
        }
        $name = empty($user) ? '' : $user;
        return $name;
    }
    public function getGoodsNameAttr($val,$data)
    {
        $goods = Db::name('shopro_order_item')->where(['order_id'=>$data['order_id']])->find();
        if(!empty($goods)){
            $title = $goods['goods_title'];
        }
        $name = empty($title) ? '' : $title;
        return $name;
    }
    public function getImageAttr($val,$data)
    {
        $goods = Db::name('shopro_order_item')->where(['order_id'=>$data['order_id']])->find();
        if(!empty($goods)){
            $title = $goods['goods_image'];
        }
        $name = empty($title) ? '' : $title;
        return $name;
    }
    public static function orderInfo($order_id)
    {
        $order = \addons\shopro\model\Order::with('item')->where('id', $order_id)->find();
        return $order;
    }

}